package com.ebeijia.web.controller;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ebeijia.common.CommonCodeDefine;
import com.ebeijia.mybatis.pojo.UserInfo;
import com.ebeijia.service.user.UserService;
import com.ebeijia.util.LoggerUtil;
import com.ebeijia.util.StringUtil;
import com.ebeijia.web.ResponseMessage;

@Controller
@RequestMapping("/user")
public class UserController {
	// 自动注入userService
	@Autowired
	private UserService userService;

	/**
	 * 根据openId 得到 useriD和 UserNO
	 */
	@SuppressWarnings("deprecation")
	@RequestMapping("getopenIdByUserNo")
	@ResponseBody
	public ResponseMessage getUserByopenId(String openId,
			HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		openId = request.getParameter("openId");
		UserInfo in = userService.getUserByopenId(openId);
		if (in != null) {
		
			return ResponseMessage.success(in);
		} else {
			return ResponseMessage.error(
					CommonCodeDefine.ERROR_NO_BIND.getCode(),
					CommonCodeDefine.ERROR_NO_BIND.getDesc());
		}
	}

	@RequestMapping("getopenId")
	@ResponseBody
	public ResponseMessage getopenId(String openId, HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		List<UserInfo> userInfos = userService.getOpenId(openId);
		if (userInfos.size() != 0) {
			return ResponseMessage.success(userInfos);
		} else {
			return ResponseMessage.error(
					CommonCodeDefine.ERROR_NO_BIND.getCode(),
					CommonCodeDefine.ERROR_NO_BIND.getDesc());
		}
	}



	@RequestMapping("/bindUserInfo")
	@ResponseBody
	public ResponseMessage bindUserInfo(UserInfo userInfo) throws Exception {

		if (StringUtils.isEmpty(userInfo.getUserNo())) {
			return ResponseMessage.error("用户编号不能为空");
		}

		if (StringUtils.isEmpty(userInfo.getPhoneNo())) {
			return ResponseMessage.error("手机号码不可以为空");
		}
		/*if (StringUtils.isEmpty(userInfo.getIdNo())) {
			return ResponseMessage.error("身份证号不可以为空");
		}*/
		return userService.bindUserHome(userInfo);
	}

}
